package com.shopping.dev.controller;

import com.shopping.dev.bo.CommonResultBO;
import com.shopping.dev.pojo.Items;
import com.shopping.dev.pojo.ItemsImg;
import com.shopping.dev.pojo.ItemsParam;
import com.shopping.dev.pojo.ItemsSpec;
import com.shopping.dev.service.ItemsService;
import com.shopping.dev.vo.CommentLevelCountsVO;
import com.shopping.dev.vo.ItemInfoVO;
import com.shopping.dev.vo.PagedGridResult;
import com.shopping.dev.vo.ShopCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

@Api(value = "商品接口", tags = "商品信息展示")
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController{


    @Autowired
    private ItemsService itemsService;

    @ApiOperation(value = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public CommonResultBO itemInfo(@ApiParam(name = "itemId", value = "商品Id", required = true) @PathVariable String itemId) {
        if (StringUtil.isEmpty(itemId)) {
            return CommonResultBO.error("商品id不能为空");
        }
        Items items = itemsService.queryItemById(itemId);
        List<ItemsImg> itemsImgs = itemsService.queryItemImgById(itemId);
        List<ItemsParam> itemsParams = itemsService.queryItemsParamById(itemId);
        List<ItemsSpec> itemsSpecs = itemsService.queryItemsSpecList(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItems(items);
        itemInfoVO.setItemImgList(itemsImgs);
        itemInfoVO.setItemSpecList(itemsSpecs);
        itemInfoVO.setItemParams(itemsParams);
        return CommonResultBO.ok(itemInfoVO);
    }

    @ApiOperation(value = "查看商品评论等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public CommonResultBO commentLevel(@ApiParam(name = "itemId", value = "商品Id", required = true) @RequestParam String itemId,
                                       @ApiParam(name = "level", value = "商品等级", required = false) @RequestParam(required = false) Integer level) {
        if (StringUtil.isEmpty(itemId)) {
            return CommonResultBO.error("商品id不能为空");
        }

        CommentLevelCountsVO commentLevelCountsVO = itemsService.queryCommentLevel(itemId, level);
        return CommonResultBO.ok(commentLevelCountsVO);
    }

    @ApiOperation(value = "查看商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public CommonResultBO comment(@ApiParam(name = "itemId", value = "商品Id", required = true) @RequestParam String itemId,
                                  @ApiParam(name = "level", value = "评价",required = false) @RequestParam Integer level,
                                  @ApiParam(name = "page", value = "第几页", required = true) @RequestParam Integer page,
                                  @ApiParam(name = "pageSize", value = "每页长度", required = true) @RequestParam Integer pageSize) {
        if (StringUtil.isEmpty(itemId)) {
            return CommonResultBO.error("商品id不能为空");
        }
        if (page == null){
            page = DEFAULT_PAGE;
        }
        if (pageSize == null){
            pageSize = DEFAULT_PAGESIZE;
        }
        PagedGridResult result = itemsService.queryComments(itemId, level, page, pageSize);
        return CommonResultBO.ok(result);
    }
    @ApiOperation(value = "查询商品", httpMethod = "GET")
    @GetMapping("/search")
    public CommonResultBO search(@ApiParam(name = "keywords", value = "关键词", required = true) @RequestParam String keywords,
                                  @ApiParam(name = "sort", value = "排序规则",required = false) @RequestParam(required = false) String sort,
                                  @ApiParam(name = "page", value = "第几页", required = true) @RequestParam(required = false) Integer page,
                                  @ApiParam(name = "pageSize", value = "每页长度", required = true) @RequestParam(required = false) Integer pageSize) {
        if (StringUtil.isEmpty(keywords)) {
            return CommonResultBO.error("关键词不能为空");
        }
        if (page == null){
            page = DEFAULT_PAGE;
        }
        if (pageSize == null){
            pageSize = DEFAULT_PAGESIZE;
        }
        PagedGridResult result = itemsService.searchItems(keywords, sort, page, pageSize);
        return CommonResultBO.ok(result);
    }
    @ApiOperation(value = "根据第三分类id查询商品", httpMethod = "GET")
    @GetMapping("/catItems")
    public CommonResultBO catItems(@ApiParam(name = "catId", value = "关键词", required = true) @RequestParam Integer catId,
                                 @ApiParam(name = "sort", value = "排序规则",required = false) @RequestParam(required = false) String sort,
                                 @ApiParam(name = "page", value = "第几页", required = true) @RequestParam(required = false,defaultValue = "1") Integer page,
                                 @ApiParam(name = "pageSize", value = "每页长度", required = true) @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
        if (catId == null) {
            return CommonResultBO.error("分类ID不能为空");
        }

        PagedGridResult result = itemsService.searchItemsThirdByCatId(catId, sort, page, pageSize);
        return CommonResultBO.ok(result);
    }

    @ApiOperation(value = "根据商品规格id查找商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public CommonResultBO refresh(@ApiParam(name = "itemSpecIds", value = "拼接规格id", required = true) @RequestParam String itemSpecIds) {
        if (itemSpecIds == null) {
            return CommonResultBO.error("分类ID不能为空");
        }

        List<ShopCartVO> list = itemsService.queryItemsBySpecIds(itemSpecIds);
        return CommonResultBO.ok(list);
    }
}
